var oprateUrl=DOMAIN +'/v1/refundType'+isLogin;
$(document).ready(function() {
	loadTable();
	//查询
	$("#searchBtn").click(function() {
		//新查询参数
		function queryParamNew(params) {
			var params = {};
			params.code = $("#code").val();
			params.name = $("#name").val();
			return params;
		};
		$('#table').bootstrapTable(
			'refreshOptions', {
				url: oprateUrl,
				//查询参数
				queryParams: queryParamNew
			}
		);

	});
});

function loadTable() {
	$('#table').bootstrapTable({
		url: oprateUrl,
		method: 'get',
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: '#toolbar',
		striped: true,
		cache: false,
		pagination: true,
		sortable: false,
		sortOrder: "asc",
		showRefresh: false,
		showColumns: false,
		minimumCountColumns: 4,
		search: false,
		queryParams: queryParams,
		toolbar: '#exampleTableEventsToolbar',
		sidePagination: "client",
		pageNumber: 1,
		pageSize: 10,
		pageList: [5, 10],
		strictSearch: true,
		clickToSelect: true,
		cardView: false,
		detailView: false,
		columns: [{
			field: 'code',
			title: "退货类型代码",
			align: "center",
			valign: "middle"
		}, {
			field: "name",
			title: "退货类型名称",
			align: "center",
			valign: "middle"
		}, {
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function(value,row,index) { 
				return '<button data-toggle="modal" data-edit="div-modal" data-target="#div-modal" class="btn btn-primary btn-sm edit" onclick="showBox();">&emsp;编辑</button> <button data-del="'+oprateUrl+'" class="btn btn-primary btn-sm del">&emsp;删除</button>';
	        } 
		}],
		onLoadSuccess: function() {
			console.log("加载成功");
		},
		onLoadError: function() {
			console.log("加载数据失败");
		},
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		  //是否显示导出按钮
		    buttonsAlign: "left",
		  //按钮位置
		    exportDataType: 'all',
		   //导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		    Icons: 'glyphicon glyphicon-export',
		    exportTypes: ['excel', 'doc', 'xlsx', 'txt', ],
		  //导出文件类型
		    exportOptions: {       //ignoreColumn: [0,1],  //忽略某一列的索引
			      
			fileName: '导出数据表',
			  //文件名称设置
			      worksheetName: 'sheet1',
			  //表格工作区名称
			      tableName: '导出数据表',
			       // excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式
			    
		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
	});
	//查询参数
	function queryParams(params) {
		var params = {};
		return params;
	};
};

//保存
function funSave(){
    var id = $("#id").val();
	var type ="";
	var data = $("#form-modal").serialize();
    if(isNotEmpty(id)){//修改
    	type="PATCH";
    }else{//新增
    	type="POST";
    }
    $.ajax({
        type : type,
        url : oprateUrl,
        data:data,
        async: false,  
        success : function(result) {
        	if (result.status == 200) {
        		$('.close').click();
        		document.getElementById("form-modal").reset();
    			$("#id").val("");
	        	$("#table").bootstrapTable("refresh", {
			     silent: true //静态刷新
			    });
			}
        	if(result.status==402){
        		alert(result.msg);
        	}
        },
        error : function() {
        	alert("修改失败！");
        }
    });
};
//保存
$("#btn-save").click(function() {
	if(isEmpty($("#pname").val())){
		alert("请填写完整信息!");
		return;
	}
	funSave();
});

//关闭
$("#btn-close").click(function() {
    document.getElementById("form-modal").reset();
    $("#id").val("");
});

$("#Btn-insert").click(function() {
	$("#id").val("");
     $("#form-modal")[0].reset();
     $("#codeBox").css("display","none");
     $("#modalTitle").text("新增");
});

function showBox(){
     $("#codeBox").css("display","block");
     $("#modalTitle").text("修改");
}
